package com.zf.yichat.service.config;

import com.zf.yichat.vo.ApiConst;
import io.rong.RongCloud;
import io.rong.methods.sensitive.SensitiveWord;
import io.rong.models.response.ListWordfilterResult;
import io.rong.models.response.ResponseResult;
import io.rong.models.sensitiveword.SensitiveWordModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author wu
 */
@Component
public class SensitiveWordService {

    // 注入配置文件中的融云API配置
    @Value("${rongcloud.key}")
    private String appKey;

    @Value("${rongcloud.secret}")
    private String appSecret;


    public RongCloud rongCloud;

    public SensitiveWordService() {

    }
    // 添加敏感词
    public Integer addSensitiveWord(String word, String replaceWord) throws Exception {
        if (null == rongCloud) {
            rongCloud = RongCloud.getInstance(appKey, appSecret);
        }

        SensitiveWord sensitiveWord = new SensitiveWord(appKey, appSecret);
        sensitiveWord.setRongCloud(rongCloud);

        SensitiveWordModel wordModel = new SensitiveWordModel();
        // 设置敏感词
        wordModel.setKeyword(word);
        // 设置替换词
        wordModel.setReplace(replaceWord);
        // 类型 1 代表 IM 即时通讯类型
        wordModel.setType(ApiConst.Number.INTEGER_ZERO);
        // 往融云服务器添加关键字
        ResponseResult result = sensitiveWord.add(wordModel);

        // 处理 result 中的信息
        return result.getCode();
    }

    // 删除敏感词
    public Integer deleteSensitiveWord(String keyword) throws Exception {
        if (null == rongCloud) {
            rongCloud = RongCloud.getInstance(appKey, appSecret);
        }
        SensitiveWord sensitiveWord = rongCloud.sensitiveword;
        ResponseResult result = sensitiveWord.remove(keyword);

        // 处理 result 中的信息
        return result.getCode();
    }


    // 查询所有敏感词
    public ListWordfilterResult getAllSensitiveWords() throws Exception {
        if (null == rongCloud) {
            rongCloud = RongCloud.getInstance(appKey, appSecret);
        }
        SensitiveWord sensitiveWord = new SensitiveWord(appKey, appSecret);
        ListWordfilterResult list = sensitiveWord.getList(1);
        return list;
    }

}
